package com.ciquan.server.controller;

import com.ciquan.server.bean.PhoneCode;
import com.ciquan.server.bean.UserInfo;
import com.ciquan.server.service.PhoneCodeService;
import com.ciquan.server.service.UserInfoService;
import com.ciquan.server.util.web.Result;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by card on 2015/8/29.
 */
@Controller
@RequestMapping("/api/userInfo/")
public class ServiceUserInfoController extends AbstractController{

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private PhoneCodeService phoneCodeService;

    @RequestMapping(value = "/register", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String register(HttpServletRequest request) {
        Result result = new Result();
        result.setSuccess(false);

        String phone = ServletRequestUtils.getStringParameter(request, "phone", "");
        String password = ServletRequestUtils.getStringParameter(request, "password", "");
        String code = ServletRequestUtils.getStringParameter(request, "code", "");

        if(phone.length() != 11) {
            result.setStatusmsg("输入的号码有误");
        }

        else if(StringUtils.isBlank(password)) {
            result.setStatusmsg("请输入密码");
        }

        else if(!phoneCodeService.validate(phone, code)) {
            result.setStatusmsg("验证码错误");
        }

        else {
            UserInfo userInfo = new UserInfo();
            userInfo.setMobile(phone);
            userInfo.setPassword(password);
            userInfoService.registerUser(userInfo, result);
        }
        return result.toString();
    }

    @RequestMapping(value = "/sendCode", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String sendCode(HttpServletRequest request) {
        Result result = new Result();
        result.setSuccess(false);
        
        String phone = ServletRequestUtils.getStringParameter(request, "phone", "");
        if(phone.length() != 11) {
            result.setStatusmsg("输入的号码有误");
        }

        else {
            PhoneCode phoneCode = new PhoneCode();
            phoneCode.setPhone(phone);
            phoneCode.setCode("" +
                    RandomUtils.nextInt(10) +
                    RandomUtils.nextInt(10) +
                    RandomUtils.nextInt(10) +
                    RandomUtils.nextInt(10));
            phoneCodeService.send(phoneCode, result);
        }
        return result.toString();
    }

    @RequestMapping(value = "/resetPassword", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String resetPassword(HttpServletRequest request) {
        Result result = new Result();
        result.setSuccess(false);

        String phone = ServletRequestUtils.getStringParameter(request, "phone", "");
        String password = ServletRequestUtils.getStringParameter(request, "password", "");
        String code = ServletRequestUtils.getStringParameter(request, "code", "");

        if(phone.length() != 11) {
            result.setStatusmsg("输入的号码有误");
        }

        else if(StringUtils.isBlank(password)) {
            result.setStatusmsg("请输入密码");
        }

        else if(!phoneCodeService.validate(phone, code)) {
            result.setStatusmsg("验证码错误");
        }

        else {
            UserInfo userInfo = new UserInfo();
            userInfo.setMobile(phone);
            userInfo.setPassword(password);
            userInfoService.resetPassword(userInfo, result);
        }

        return result.toString();
    }
}
